package it.hansir.sonic_tbs;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.jingewenku.abrahamcaijin.commonutil.AppToastMgr;

public class WebActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int PERMISSION_REQUEST_CODE_STORAGE = 10001;
    private static final int PERMISSION_REQUEST_CODE_ACCONT = 10002;
    private Button but,button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        but = (Button) findViewById(R.id.button3);
        button = (Button) findViewById(R.id.button4);


        but.setOnClickListener(this);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button3:
                if (hasTPermission()) {
                    LogUtil.d(this,"有第一权限");
                    AppToastMgr.ToastShortCenter(this,"下一页");
                }else {
                    LogUtil.d(this,"没有有第一权限");
                    requestTPermission();
                }
                break;
            case R.id.button4:
                if (hasSecondPermission()) {
                    LogUtil.d(this,"有第二权限");
                    AppToastMgr.ToastShortCenter(this,"第二页");
                }else {
                    LogUtil.d(this,"没有有第二权限");
                    requestSecPermission();
                }
                break;
        }
    }


    public boolean hasTPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            LogUtil.d(this,"是否有第一权限");
            return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
                    PackageManager.PERMISSION_GRANTED ;
        }
        return true;
    }

    private void requestTPermission() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            LogUtil.d(this,"请求第一个权限");
            requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE_STORAGE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
       switch (requestCode) {
           case PERMISSION_REQUEST_CODE_STORAGE:
            if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                LogUtil.d(this,"还没有获取第一个权限");
                AppToastMgr.ToastShortCenter(this,"请求设置页面获取第一个权限");
            } else {
                LogUtil.d(this,"已经有第一个权限了");
            }
            break;
           case PERMISSION_REQUEST_CODE_ACCONT:

               if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                   LogUtil.d(this,"还没有获取第二个权限");
                   AppToastMgr.ToastShortCenter(this,"请求设置页面获取第二个权限");
               } else {
                   LogUtil.d(this,"已经有第二个权限了");
//                   获取成功
               }
               break;
       }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }


    public boolean hasSecondPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            LogUtil.d(this,"是否有第二权限");
            return ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) ==
                    PackageManager.PERMISSION_GRANTED ;
        }
        return true;
    }

    private void requestSecPermission() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            LogUtil.d(this,"请求第二个权限");
            requestPermissions(new String[] {Manifest.permission.GET_ACCOUNTS}, PERMISSION_REQUEST_CODE_ACCONT);
        }
    }
}
